import { reactive, ref, inject } from "vue";
import { useProjectStore } from '../../../store/useProjectStore'

interface Tree {
    id: string;
    label: string;
    value: string;
    children?: Tree[];
}

export const useProjectSchedule = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;

    const scheduleData = reactive(
        {
            total: 0,
            tableData: [],
            queryCondition: {
                projectName: '',
                status: null,
            },
            tree: ref<Tree[]>([])
        }
    );

    const projectStore = useProjectStore();

    //编制进度计划
    const formulateSchedule = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.FormulateProjectSchedule,
                projectStore.scheduleModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    //根据项目ID获取项目进度计划
    const getScheduleDetailByProjectId = async (projectId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetScheduleDetailByProjectId,
                { projectId: projectId }
            );
            if (response.status == "200" && response.data.code == "200") {
                projectStore.scheduleModel = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }

    //根据项目ID获取项目进度计划基本信息
    const getScheduleEntityByProjectId = async (projectId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetScheduleEntityByProjectId,
                { projectId: projectId }
            );
            if (response.status == "200" && response.data.code == "200") {
                projectStore.scheduleModel = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }

     //根据进度ID获取进度详情
     const getScheduleDetailByScheduleId = async (sId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetScheduleDetailByScheduleId,
                { scheduleId: sId }
            );

            if (response.status == "200" && response.data.code == "200") {
                projectStore.scheduleModel.tasks = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }

    //查询
    const getProjectSchedules = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.GetProjectSchedules,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { projectName: scheduleData.queryCondition.projectName, status: scheduleData.queryCondition.status },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                scheduleData.tableData = response.data.data;
                scheduleData.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };

    //更新项目进度计划指定信息
    const updateScheduleMainSpecificContent = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.UpdateScheduleMainSpecificContent,
                projectStore.scheduleModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    //根据进度计划主记录ID获取进度计划
    const getScheduleDetailById = async (id) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetScheduleDetailById,
                { id: id }
            );

            if (response.status == "200" && response.data.code == "200") {
                projectStore.scheduleModel = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }

    //根据需求ID获取进度任务
    const getScheduleTaskByRId = async (rId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetScheduleTaskByRId,
                { rId: rId }
            );

            if (response.status == "200" && response.data.code == "200") {
                projectStore.taskModel = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }

    //根据项目ID获取项目进度计划树
    const getProjectScheduleTree = async (projectId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetProjectScheduleTree,
                {
                    projectId: projectId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                const transformOrgData: any = (childTasks: any[]) => {
                    return childTasks.map((task) => {
                        return {
                            id: task.id,
                            label: task.taskName,
                            value: task.taskName,
                            children: task.childTasks
                                ? transformOrgData(task.childTasks)
                                : [],
                        };
                    });
                };
                scheduleData.tree = transformOrgData(response.data.data);
            }
            console.log('scheduleData.tree',scheduleData.tree)
        } catch (error) {
            console.error(error);
        }
    };

    return {
        scheduleData,
        projectStore,
        formulateSchedule,
        getScheduleDetailByProjectId,
        getScheduleEntityByProjectId,
        getScheduleDetailByScheduleId,
        getProjectSchedules,
        updateScheduleMainSpecificContent,
        getScheduleDetailById,
        getScheduleTaskByRId,
        getProjectScheduleTree
    };
};